from selenium import webdriver
from selenium.webdriver.common.by import By
import time


class Test:
    def __init__(self):
        # 打开浏览器 ,请求163邮箱
        self.driver = webdriver.Chrome()
        self.driver.get('https://mail.163.com/')

        # 准备数据
        self.userinfo = {
            'username': 'jh135****3364',
            'email': 'jh135****3364@163.com',
            'password': 'Jh196322'
        }

    def login(self):
        login = self.driver.find_element(By.TAG_NAME, 'iframe')
        self.driver.switch_to.frame(login)

        # 输入账号
        self.driver.find_element(By.NAME, 'email').click()
        self.driver.find_element(By.NAME, 'email').send_keys('jh135****3364')
        # 输入密码
        self.driver.find_element(By.NAME, 'password').click()
        self.driver.find_element(By.NAME, 'password').send_keys('Jh196322')
        # 点击登录
        self.driver.find_element(By.ID, 'dologin').click()

    def post(self):
        # 点击写信
        time.sleep(2)
        bth = self.driver.find_element(By.XPATH, '//*[@id="_mail_component_149_149"]/span[2]').click()
        bth = self.driver.implicitly_wait(5)
        # 点击收信人 输入收件人邮箱
        self.driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('137****1794@163.com')
        # 点击主题输入内容,    nui-ipt-input
        self.driver.find_elements(By.CLASS_NAME, 'nui-ipt-input')[2].send_keys('学霸')
        # iframe 切换
        iner = self.driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
        frame1 = self.driver.switch_to.frame(iner)

        # 点击内容编写
        self.driver.find_element(By.XPATH, '//body[@class="nui-scroll"]').send_keys('晚上见')
        # 切回主页frame
        time.sleep(2)
        self.driver.switch_to.default_content()
        # 点击发送
        time.sleep(5)
        self.driver.find_element(By.ID,'_mail_button_8_280').click()

    def quit(self):
        time.sleep(2)
        self.driver.quit()


if __name__ == '__main__':
    t1 = Test()
    t1.login()
    t1.post()
    t1.quit()
